package com.example.healthmanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.healthmanagement.entity.ChildSportRecords;
import com.example.healthmanagement.pojo.Foodinfo;
import com.example.healthmanagement.pojo.VO.FoodinfoVo;
import com.example.healthmanagement.service.FoodinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2024-03-08
 */
@RestController
@RequestMapping("/foodinfo")
public class FoodinfoController {

    @Autowired
    FoodinfoService foodinfoService;

    @GetMapping("")
    public List<Foodinfo> getSportRecords( Foodinfo foodinfo) {
        List<Foodinfo> objectList = foodinfoService.list(new LambdaQueryWrapper<Foodinfo>().eq(foodinfo.getFoodname() != null, Foodinfo::getFoodname, foodinfo.getFoodname())
        .eq(foodinfo.getCategory() != null, Foodinfo::getCategory, foodinfo.getCategory())
                        .eq(foodinfo.getEnergy() != null, Foodinfo::getEnergy, foodinfo.getEnergy())
        );

        return objectList;
    }


    @GetMapping("/getList")
    public List<FoodinfoVo> getAllWrapper(){
        LambdaQueryWrapper<Foodinfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Foodinfo::getPId, 0);
        wrapper.select(Foodinfo::getCategory);
        wrapper.groupBy(Foodinfo::getCategory);
        //查询根级
        List<Foodinfo> regions = foodinfoService.list(wrapper);

        List<FoodinfoVo> list = regions.stream().map(p -> {
            FoodinfoVo obj = new FoodinfoVo();
            BeanUtils.copyProperties(p, obj);
            return obj;
        }).collect(Collectors.toList());

        list.forEach(this::getChildren);
        return list;
    }

    private void getChildren(FoodinfoVo item){
        LambdaQueryWrapper<Foodinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Foodinfo::getCategory, item.getCategory());
        //根据parentId查询
        List<Foodinfo> list = foodinfoService.list(wrapper);

        List<FoodinfoVo> voList = list.stream().map(p -> {
            FoodinfoVo vo = new FoodinfoVo();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        //写入到children
        item.setChildren(voList);

        //如果children不为空，继续往下找
//        if (!CollectionUtils.isEmpty(voList)) {
//            voList.forEach(this::getChildren);
//        }
    }

    @PostMapping("")
    public boolean saveSportRecords(@RequestBody Foodinfo foodinfo) {

        return foodinfoService.save(foodinfo);
    }
    @PutMapping ("")
    public boolean updateSportRecords(@RequestBody Foodinfo foodinfo) {

        return foodinfoService.updateById(foodinfo);
    }
    @DeleteMapping  ("/{id}")
    public boolean DeleteSportRecords(@PathVariable("id") Long[] id) {

        return foodinfoService.removeByIds(Arrays.asList(id));
    }

}

